<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/7/26
 * Time: 17:40
 */

namespace Api\Service;


class ScoreCommentService
{
    /**
     * 添加评论
     */
    public function addComment($param)
    {
        $model = M('ScoreComment');
        $res = $model->create($param);
        if ($res) {
            $param['user_id'] = $param['uid'];
            $id = $model->add($param);

            //谱单评论数记录+1
            M('Score')->where(array('id'=>$param['score_id']))->setInc('comment_count');

            return $id;
        } else {
            E($model->getError());
        }
    }


    /**
     * 获取谱单评论内容
     */
    public function getComment($param)
    {
        $model = M('ScoreComment');
        $scoreId = $param['score_id'];
        $map = array(
            'score_id' => $scoreId,
            'is_delete' => 0
        );

        $count = $model->where($map)->count();
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];
        $page = new PageService($param, $count, $listRows);
        $show = $page->show();

        $list = $model->where($map)
            ->order('create_time desc')->limit($page->firstRow.','.$page->listRows)
            ->select();
        //获取用户信息
        $userIds = array_column($list, 'user_id');
        $userService = new UserService();
        $userInfo = $userService->getMapByUids($userIds);
        //获取谱单信息
        $score = M('Score')->where(array('id' => $scoreId))
            ->field('id,name,creator,create_time,is_private, author_type')->select();

        $scoreService = new ScoreService();
        $scoreInfo = $scoreService->convertList($score);

        $commentList['score']['id'] = $scoreInfo[0]['id'];
        $commentList['score']['name'] = $scoreInfo[0]['name'];
        $commentList['score']['creator'] = $scoreInfo[0]['creator'];
        foreach ($list as $key => $val) {
            $commentList['list'][$key]['id'] = $val['id'];
            $commentList['list'][$key]['user'] = $userInfo[$val['user_id']];
            $commentList['list'][$key]['remark'] = $val['remark'];
            $commentList['list'][$key]['time'] = $val['create_time'];
        }
        if (empty($commentList['list'])) {
            $commentList['list'] = array();
        }
        $commentList['nowPage'] = $page->nowPage;
        $commentList['totalCount'] = $count;
        $commentList['totalPage'] = ceil($count / $listRows);
        return $commentList;
    }

    /**
     * 删除评论
     */
    public function delComment($param)
    {
        $model = M('ScoreComment');
        $cond = array('id'=>$param['comment_id']);
        $data = array(
            'is_delete' => 1
        );
        $count = $model->where($cond)->count();
        if (empty($count)) {
            return true;
        }
        $id = $model->where($cond)->save($data);

        //谱单评论数记录-1
        M('Score')->where(array('id'=>$param['score_id']))->setDec('comment_count');

        return $id;
    }


    /**
     * 通过谱单id集获取评论
     * @param $scoreIds
     * @param $param
     * @param string $listRows
     * @return array
     */
    public function getCommentByScoreIds($scoreIds, $param, $listRows='')
    {
        $model = M('ScoreComment');
        $map = array(
            'is_delete' => 0,
            'score_id' => array('in', $scoreIds)
        );
        //分页
        $count = $model->where($map)->count();
        $listRows = empty($listRows) ? 20 : $listRows;
        $page = new PageService($param, $count, $listRows);
        $show = $page->show();
        $list = $model->where($map)->order('create_time desc')
            ->limit($page->firstRow.','.$page->listRows)->select();
        //获取用户信息
        $userIds = array_column($list, 'user_id');
        $userService = new UserService();
        $userInfo = $userService->getMapByUids($userIds);

        //获取谱单信息
        $score = M('Score')->where(array('score_id' => array('in', $scoreIds)))
            ->field('id, name, creator')->select();
        foreach ($score as $key => $val) {
            $scoreInfo[$val['id']] = $val;
        }

        $commentMap = array();
        foreach ($list as $key => $val) {
            $commentMap['list'][$key]['id'] = $val['id'];
            $commentMap['list'][$key]['user'] = $userInfo[$val['user_id']];
            $commentMap['list'][$key]['remark'] = $val['remark'];
            $commentMap['list'][$key]['time'] = $val['create_time'];
            $commentMap['list'][$key]['score'] = $scoreInfo[$val['score_id']];
        }
        $commentMap['nowPage'] = $page->nowPage;
        $commentMap['totalCount'] = $count;
        $commentMap['totalPage'] = ceil($count / $listRows);
        return $commentMap;
    }
}